package com.inputabc.ct.v1.ui.keysetting.action;

import com.inputabc.EzGUIFramework.util.alias.Ez;
import com.inputabc.ct.v1.context.ContextParams;
import com.inputabc.ct.v1.setting.KeySetting;
import com.inputabc.ct.v1.setting.SettingContext;
import com.inputabc.ct.v1.ui.keysetting.KeySettingBuilder;
import com.inputabc.ct.v1.ui.keysetting.component.KeySettingBaiduPanel;
import com.inputabc.ct.v1.ui.keysetting.component.KeySettingControlPanel;
import com.inputabc.ct.v1.ui.keysetting.component.KeySettingFrame;
import com.inputabc.ct.v1.ui.keysetting.component.KeySettingYoudaoPanel;
import com.inputabc.ct.v1.util.KeyIOUtils;

import javax.swing.*;
import java.awt.event.ActionEvent;

/**
 * KeySettingControlPanel对象的事件处理类
 *
 * @author gaoweiyi
 */
public class KeySettingControlPanelAction {
    private KeySettingControlPanel keySettingControlPanel;
    private KeySetting keySetting = SettingContext.keySetting;

    public KeySettingControlPanelAction(KeySettingControlPanel keySettingControlPanel) {
        this.keySettingControlPanel = keySettingControlPanel;
    }

    public void clickApply(ActionEvent ae) {
        JTextField appKeyTextField = (JTextField) Ez.getComponentWithCrossParentByNameEqual(keySettingControlPanel, KeySettingYoudaoPanel.class.getName() + "-appKeyTextField");
        JTextField appSecretTextField = (JTextField) Ez.getComponentWithCrossParentByNameEqual(keySettingControlPanel, KeySettingYoudaoPanel.class.getName() + "-appSecretTextField");
        JTextField appIdTextField = (JTextField) Ez.getComponentWithCrossParentByNameEqual(keySettingControlPanel, KeySettingBaiduPanel.class.getName() + "-appIdTextField");
        JTextField securityKeyTextField = (JTextField) Ez.getComponentWithCrossParentByNameEqual(keySettingControlPanel, KeySettingBaiduPanel.class.getName() + "-securityKeyTextField");

        keySetting.getYoudao().setAppKey(appKeyTextField.getText().trim());
        keySetting.getYoudao().setAppSecret(appSecretTextField.getText().trim());
        keySetting.getBaidu().setAppId(appIdTextField.getText().trim());
        keySetting.getBaidu().setSecurityKey(securityKeyTextField.getText().trim());
        KeyIOUtils.store();//将密钥信息持久化到文件
        JButton applyButton = (JButton) ae.getSource();
        applyButton.setEnabled(false);
    }

    public void clickCancel() {
        KeySettingFrame keySettingFrame = (KeySettingFrame) Ez.getComponentWithCrossParentByNameEqual(keySettingControlPanel, KeySettingFrame.class.getName());
        keySettingFrame.dispose();
        KeySettingBuilder keySettingBuilder = (KeySettingBuilder) ContextParams.contextParam.get("keySettingBuilder");
        keySettingBuilder.destory();
    }

    public void clickOk(ActionEvent ae) {
        clickApply(ae);
        clickCancel();
    }

}
